<?php
/*
 * @Descripttion: 搜索词
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-11-27 09:51:22
 * @LastEditTime: 2019-12-02 13:30:39
 */
namespace app\admin\controller;

use app\admin\model\Searchword as SearchwordModel;
use tool\Log;

class Searchword extends Base
{
    // 搜索词列表
    public function index()
    {
        if(request()->isAjax()) {

            $limit = input('param.limit');
            $keyword = input('param.keyword');
            $ism = input('param.ism');
            $where = [];
            if (!empty($keyword)) {
                $where[] = ['keyword', 'like', $keyword . '%'];
            }
            if (!empty($ism)) {
                $where[] = ['ism', 'eq', $ism];
            }

            $model = new SearchwordModel();
            $list = $model->getSearchword($limit, $where);

            if(0 == $list['code']) {

                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }

            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }

        return $this->fetch();
    }

    // 编辑
    public function edit()
    {
        if (request()->isAjax()) {
            
            $param = input('post.');

            if (empty($param['Keyword'])) {
                return reMsg(-1, '', '请输入关键词');
            }

            $model = new SearchwordModel();
            $res = $model->editSearchword($param);

            Log::write("编辑搜索词：" . $param['Keyword']);

            return json($res);
        }

        $id = input('param.id');

        $model = new SearchwordModel();

        $this->assign([
            'searchword' => $model->getSearchwordById($id)['data'],
        ]);

        return $this->fetch();
    }

    // 删除
    public function delete()
    {
        if (request()->isAjax()) {

            $id = input('param.id');

            $model = new SearchwordModel();
            $res = $model->delSearchwordById($id);

            Log::write("删除搜索词：" . $id);

            return json($res);
        }
    }

}